package com.seedwill.common.util;

import java.io.UnsupportedEncodingException;

public class StringHelper {
    public static final String empty = "";

    public static final String emptySpace = " ";

    public static final String zero = "0";

    public static String rendCodeLeft(String code, int minLen, String fill) {
        code = null2Empty(code);
        if (code.length() >= minLen) {
            return code;
        } else {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < (minLen - code.length()); i++) {
                sb.append(fill);
            }
            sb.append(code);
            return sb.toString();
        }
    }
    public static String null2Empty(String obj) {
        if (null == obj) {
            return empty;
        } else {
            return obj;
        }
    }
    public static String null2EmptyN(String obj) {
        if (null == obj) {
            return empty;
        } else {
            return obj.trim();
        }
    }
    public static String null2Empty(Object obj) {
        if (null == obj) {
            return empty;
        } else {
            if (obj instanceof String) {
                return ((String) obj);
            } else {
                return obj.toString();
            }
        }
    }
    public static String null2EmptyN(Object obj) {
        if (null == obj) {
            return empty;
        } else {
            if (obj instanceof String) {
                return ((String) obj).trim();
            } else {
                return obj.toString().trim();
            }
        }
    }
    public static String null2EmptySpace(Object obj) {
        if (null == obj) {
            return emptySpace;
        } else {
            return obj.toString().trim();
        }
    }

    public static String cleanStrN(String str) {
        if (null != str) {
            str = str.trim();
            str.replace("\\n", empty);
            str.replace("\\r", empty);
            str.replace("\\t", empty);
            // str.replace("\\r\n", empty);
            str.replace("\\x0B", empty);
            return str;
        } else {
            return empty;
        }
    }

    public static String stringTransform(String str, String oldEncode,
            String newEncode) {

        String resule = "";
        try {
            byte[] strbyte = str.getBytes(oldEncode);
            resule = new String(strbyte, newEncode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return resule;
    }

    public static String stringTransformUTF8(String str) {

        return StringHelper.stringTransform(str, "ISO8859-1", "UTF-8");
    }

    public static String stringTransformGB2312(String str) {

        return StringHelper.stringTransform(str, "ISO8859-1", "GB2312");
    }

    public static String stringTransformGBK(String str) {

        return StringHelper.stringTransform(str, "ISO8859-1", "GBK");
    }

    public static String textareaTransformHtml(String str) {
        
        str = str.replaceAll(">", "&gt;");
        str = str.replace("'", "''");
        str = str.replace(" ", "&nbsp;");
        str = str.replace("\n", "<br>");
        str = str.replace("\r\n", "<br>");
        return str;
    }

    public static String htmlTransformTextarea(String str) {

        str = str.replaceAll("&gt;", ">");
        str = str.replace("''", "'");
        str = str.replace("&nbsp;", " ");
        str = str.replace("<br>", "\n");
        // str   =   str.replace("<br>","\r\n") ; linux
        return str;
    }
    //	public static void main(String[] args){
    //		String a = rendCodeLeft("22221", 4, "0");
    //		System.out.println(a);
    //	}
    public static String null2EmptyMb(Object obj)
    {
        if(null == obj)
        {
            return "";
        } else
        {
            String s = obj.toString().trim().replaceAll("\u951F\u65A4\u62F7+| +", " ");
            return s.replaceAll(" \u951F\uFFFD0\u951F\uFFFD2", "");
        }
    }
	public static String parseStrLimit(String title, int limit) {
		// TODO Auto-generated method stub
		title = null2Empty(title);
		return title.length()>limit?title.substring(0, limit):title;
	}
	
    public static String toHtml(String sStr)
    {
        if(null == sStr || sStr.trim().equals("")){
            return "";
        }
        if(sStr.toLowerCase().indexOf("<br>") < 0)
        {
            StringBuffer sTmp = new StringBuffer();
            for(int i = 0; i <= sStr.length() - 1; i++){
                if('\n' == sStr.charAt(i)){
                    sTmp = sTmp.append("<br>");
                }else if(' ' == sStr.charAt(i)){
                    sTmp = sTmp.append("&nbsp;");
                }else{
                    sTmp = sTmp.append(sStr.substring(i, i + 1));
                }
            }
            return sTmp.toString();
        } else {
            return sStr;
        }
    }
	public static boolean isNotEmpty(Object obj) {
		return null != obj && obj.toString().trim().length()>0;
	}
	public static boolean isEmpty(Object obj) {
		return null != obj && obj.toString().equals(empty);
	}
}
